import re


def get_method_html(clas_in, ver_in):
    class_now = ''
    ver_now = ''
    is_done = False
    res_list = []
    for line in open('in_method.txt', encoding='utf-8'):
        re_obj = re.match(r'(.*?)\t(.*?)\t(.*?)\t(.*?)$', line)
        if not re_obj:
            print('cant match line:', line)
        clas, ver, obj, typ = re_obj.group(1, 2, 3, 4)
        if clas and clas != class_now:
            class_now = clas
            ver_now = ver
        if  class_now == clas_in and ver_now == ver_in:
            if obj:
                res_list.append(f'{obj}:{typ}')
            is_done = True
            continue
        if is_done:
            break
    if not is_done:
        print(f'class{clas_in}, ver{ver_in} method not found.')
    return f'\t<Method>{"|".join(res_list)}</Method>\n'


if __name__ == '__main__':
    out_str = ''
    class_now = ''
    ver_now = ''
    is_first = True
    for line in open('in_attr.txt', encoding='utf-8'):
        re_obj = re.match(r'(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t\t.*?\t(.*?)$', line)
        if not re_obj:
            print('cant match line:', line)
        clas, ver, obj, typ, mi, ma, de, cho = re_obj.group(1, 2, 3, 4, 5, 6, 7, 8)
        if cho:
            cho = cho.strip().replace('\t', '|')
        if clas and clas != class_now:
            if is_first:
                is_first = False
            else:
                out_str += get_method_html(class_now, ver_now)
                out_str += '</Class>\n\n'
            class_now = clas
            ver_now = ver
            out_str += '<Class name="{}" Ver="{}">\n'.format(clas, ver)
        out_str += '\t<Object value="{}" Min="{}" Max="{}" Default="{}">\n'.format(obj, mi, ma, de)
        out_str += '\t\t<Choice value="{}">{}</Choice>\n'.format(cho, typ)
        out_str += '\t</Object>\n'
    out_str += '</Class>\n'
    with open('out.txt', 'w', encoding='utf-8') as outfile:
        outfile.write(out_str)
